package _11_整理题目._整理基础._集合源码;

import org.junit.Test;

import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class _Loop_Iterator {
    private List<String> list = new LinkedList() {{
        add("zhao");
        add("qian");
        add("sun");
        add("li");
    }};
    
    @Test
    public void testLoop() {
        /**
         * java 循环遍历的方式
         * 1、while()  do...while()  for(;;)  for-each 增强for(:)【只用于遍历，不能修改原数据】
         * 2、迭代器：
         *      list.iterator()
         *      list.forEach()
         *      
         */
        
        
        // for-Each 增强型for循环
        for (String str : list) {
            System.out.println(str);
        }
        
        // foreach (传入lambda表达式)
        list.forEach(System.out::println);
        
        // iterator
        Iterator iterator = list.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
        
    }
    
    @Test
    public void test() {
        HashMap<Integer,String> user = new HashMap<>();
        //往HashMap里面存放数据
        System.out.println(user.put(1, "张三"));
        System.out.println(user.put(1, "李四"));
        System.out.println(user.put(1, "王五"));
    
        System.out.println(user.get(1));

    }
}
